<?php
#订单详情
require('connect.php');
session_start();
if(isset($_SESSION['userName'])){
    $name = $_SESSION['userName'];
}else{
    echo "<script>alert('您还未登陆');location.href='login.php';</script>";
}
$res = mysqli_query($link,"select id from tb_user where username = '$name'");
$id = mysqli_fetch_array($res)['id'];
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>订单详情</title>
    <link rel="stylesheet" href="css/orders.css" />
  </head>
  <body>
    <table cellspacing="0">
        <caption><?php echo $name;?>的订单信息</caption>
        <tr>
            <th>订单号</th>
            <th>收货人</th>
            <th>送货方式</th>
            <th>下单时间</th>
            <th>订单金额</th>
            <th>订单状态</th>
            <th>操作</th>
        </tr>
        <?php
            $res2 = mysqli_query($link,"select * from tb_dingdan where orderpersonid = '$id'");
            $total = mysqli_num_rows($res2);
            $pageSize = 4;
            if($total % $pageSize == 0){
                $numOfPage = (int)($total / $pageSize);
            }else{
                $numOfPage = (int)($total / $pageSize) + 1;
            }
            if(isset($_GET['page'])){
                $currentPage = $_GET['page'];
                $start = ($currentPage - 1) * $pageSize;
            }else{
                $currentPage = 1;
                $start = 0;
            }
            $query = "select * from tb_dingdan where orderpersonid = '$id' limit $start, $pageSize";
            $res3 = mysqli_query($link,$query);
            if(mysqli_num_rows($res3) > 0){
                while($row = mysqli_fetch_array($res3)){ 
        ?>
        <tr>
            <td><?php echo $row['id'];?></td>
            <td><?php echo $row['consignee'];?></td>
            <td><?php echo $row['method'];?></td>
            <td><?php echo $row['orderdate'];?></td>
            <td>￥<span class="money"><?php echo $row['amount'];?></span></td>
            <td><?php echo $row['status'];?></td>
            <td>
                <a href="#">取消订单</a>
            </td>
        </tr>
        <?php
                }
            }else{
                echo "<tr><td colspan='7'>暂无订单信息</td></tr>";
            }
        ?>
    </table>
    <div class="page">
        <p>共<?php echo $total;?>条记录&nbsp;&nbsp;目前第<?php echo $currentPage;?>页/共<?php echo $numOfPage;?>页</p>
        <?php
            if($numOfPage>1 && $currentPage>1){
                echo "<a href = 'orders.php?id=$id&page=1' style='font-size:1.1rem;color:blue;margin-right:10px;'>首页</a>";
                echo "<a href = 'orders.php?id=$id&page=".($currentPage-1)."'>上一页</a>";
            }
            if($numOfPage>1 && $currentPage<$numOfPage){
                echo "<a style='margin-left:5px;' href = 'orders.php?id=$id&page=".($currentPage+1)."'>下一页</a>";
                echo "<a style='font-size:1.1rem;color:blue;margin-left:10px;' href = 'orders.php?id=$id&page=".$numOfPage."'>尾页</a>";
            }
        ?>
    </div>
  </body>
</html>
